home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / examples / helpsigs / makebase.sml < prev    next >
Encoding:
Text File  |  1997-08-18  |  4.3 KB  |  118 lines  |  [TEXT/R*ch]

  1. (* makebase -- create the signature database from the Moscow ML 
  2.  * library signatures.  PS 1995-11-19, 1996-04-10, 1997-07-21
  3.  *)
  4.  
  5. (* The version number inserted in generated files: *)
  6. val version = "Moscow ML 1.42";
  7.  
  8. (* Default directory containing the signature files: *)
  9. val libdirDef = "../../lib/"
  10.  
  11. (* Default filename for the resulting help database: *)
  12. val helpfileDef = "helpsigs.val"
  13.  
  14. (* Default filename for the ASCII format database: *)
  15. val txtIndexDef = "index.txt"
  16.  
  17. (* Default filename for the LaTeX format database: *)
  18. val texIndexDef = "index.tex"
  19.  
  20. (* Default directory for signatures in HTML format: *)
  21. val htmlDirDef = "htmlsigs"
  22.  
  23. (* Default filename for the HTML format database: *)
  24. val htmlIndexDef = htmlDirDef ^ "/idIndex.html"
  25.  
  26. (* Signatures not to be included in the help database: *)
  27. val stoplist = ["Misc", "Strbase"];
  28.  
  29. (* To make the database, sort entries on normalized (lower case) name,
  30.  * lump together equal normalized names in entry lists, and sort these by
  31.  * structure name (except that a Str entry always precedes the others): 
  32.  *)
  33.  
  34. fun mkbase (entries : Database.entry list) =
  35.     let open Database
  36.     fun caseless(c1, c2) = Char.compare(Char.toLower c1, Char.toLower c2)
  37.     val caseless = String.collate caseless
  38.     fun compname (e1, e2) = 
  39.         caseless (getname e1, getname e2)
  40.     val entries = Listsort.sort compname entries
  41.     fun lexico comp1 comp2 arg =
  42.         case comp1 arg of
  43.         EQUAL => comp2 arg
  44.           | res   => res
  45.     fun tagorder Str     = 0
  46.       | tagorder (Val _) = 1
  47.       | tagorder (Typ _) = 2
  48.       | tagorder (Exc _) = 3
  49.       | tagorder (Con _) = 4
  50.     fun compCompare (e1 as {comp=c1, ...}, e2 as {comp=c2, ...}) =
  51.         if tagorder c1 < tagorder c2 then LESS
  52.         else if tagorder c1 > tagorder c2 then GREATER
  53.         else String.compare(getname e1, getname e2)
  54.     fun strCompare (e1, e2) = String.compare(#str e1, #str e2)
  55.     val entryCompare = lexico compCompare strCompare
  56.     fun toLower s = 
  57.         let open Char CharVector
  58.         in tabulate(size s, fn i => toLower(sub(s, i))) end
  59.     fun lump [] = []
  60.       | lump (x1 :: xr) = 
  61.         let fun mkLump lumpname lump = (toLower (getname lumpname), 
  62.                         Listsort.sort entryCompare lump)
  63.         fun h lump1name lump1 []       = [mkLump lump1name lump1]
  64.           | h lump1name lump1 (x1::xr) = 
  65.             if compname(x1, lump1name) = EQUAL then 
  66.             h lump1name (x1 :: lump1) xr
  67.             else
  68.             mkLump lump1name lump1 :: h x1 [x1] xr
  69.         in h x1 [x1] xr end
  70.     val lumps = lump entries : (string * entry list) list
  71.     fun mkOrderedTree xs = 
  72.         let fun h 0 xs = (Empty, xs)
  73.           | h n xs =
  74.             let val m = n div 2
  75.             val (t1, (key, value) :: yr) = h m xs
  76.             val (t2, zs)                 = h (n-m-1) yr
  77.             in (Node(key, value, t1, t2), zs) end
  78.         in #1 (h (length xs) xs) end
  79.     in
  80.     mkOrderedTree lumps
  81.     end
  82.  
  83. fun dirToBase (dir, filename) =
  84.     let val res = List.foldl (Parsspec.processfile stoplist dir) 
  85.                          [] (Mosml.listDir dir)
  86.     val _ = print ("\nProcessed " ^ Int.toString (length res) 
  87.                ^ " entries in total.\n");
  88.     val _ = print ("Building database...\n");
  89.     val db = mkbase res
  90.     val _ = print ("Writing database to file " ^ filename ^ "\n");
  91.     in 
  92.     Database.writebase(filename, db)
  93.     end
  94.     handle exn as OS.SysErr (str, _) => (print(str ^ "\n\n"); raise exn)
  95.  
  96. fun process (libdir, helpfile, txtIndex, texIndex, htmldir, htmlIndex) =
  97.     (print ("Reading signatures in directory " ^ libdir ^ 
  98.         "\nand writing help database in file " ^ helpfile ^ "\n");
  99.      dirToBase (libdir, helpfile);
  100.      print ("\nWriting ASCII signature index in file " ^ txtIndex ^ "\n");
  101.      Printbase.printASCIIBase(helpfile, txtIndex);
  102.      print ("\nWriting Latex signature index in file " ^ texIndex ^ "\n");
  103.      Printbase.printLatexBase(helpfile, texIndex);
  104.      print ("\nCreating HTML versions of signature files\n");
  105.      Htmlsigs.sigsToHtml version stoplist (libdir, htmldir);
  106.      print ("\nWriting HTML signature index in file " ^ htmlIndex ^ "\n");
  107.      Htmlsigs.printHTMLBase version (helpfile, htmlIndex))
  108.  
  109. val _ = 
  110.     case Mosml.argv () of
  111.     [_]                   => 
  112.         process (libdirDef, helpfileDef, 
  113.              txtIndexDef, texIndexDef, htmlDirDef, htmlIndexDef)
  114.       | [_, libdir, helpfile, txtIndex, texIndex, htmlDir, htmlIndex] => 
  115.         process (libdir, helpfile, txtIndex, texIndex, htmlDir, htmlIndex)
  116.       | _ => print "Usage: makebase libdir helpfile \
  117.                     \txtIndex texIndex htmlDir htmlIndex\n"
  118.